﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security.AccessControl;
using System.Text.RegularExpressions;
using System.Xml;
using YanZhiwei.DotNet2.Utilities.Base;
namespace YanZhiwei.DotNet2.Utilities.Common
{
    /// <summary>
    /// 文件以及文件夹操作帮助类
    /// </summary>
    public static class FileHelper
    {
        /// <summary>
        /// 创建文件路径
        /// <para>eg:FileHelper.CreatePath(@"C:\aa\cc\dd\ee.xml");</para>
        /// </summary>
        /// <param name="path">需要创建的路径</param>
        /// <returns>是否创建成功</returns>
        public static bool CreatePath(string path)
        {
            bool _result = true;
            if (!string.IsNullOrEmpty(path) && !File.Exists(path))
            {
                try
                {
                    string _directory = GetExceptName(path);
                    CreateDirectory(_directory);
                    File.Create(path);
                }
                catch (Exception)
                {
                    _result = false;
                }
            }
            return _result;
        }
        #region  从路径中获取文件名称（不包括后缀）
        /// <summary>
        /// 从路径中获取文件名称（不包括后缀）
        /// <para>eg:FileHelper.GetFileNameOnly(@"C:\yanzhiwei.docx");==>yanzhiwei</para>
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns>文件名称（不包括后缀）</returns>
        public static string GetFileNameOnly(string path)
        {
            Match _result = null;
            string _fileName = string.Empty;
            if (!string.IsNullOrEmpty(path))
            {
                if (RegexHelper.IsMatch(path, RegexPattern.FileCheck, out _result))
                    _fileName = _result.Result("${fname}") + _result.Result("${namext}");
            }
            return _fileName;
        }
        #endregion
        #region 从路径中获取文件名称（包括后缀）
        /// <summary>
        /// 从路径中获取文件名称（包括后缀）
        /// <para>eg:FileHelper.GetFileName(@"C:\yanzhiwei.docx");==>yanzhiwei.docx</para>
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns>文件名称（包括后缀）</returns>
        public static string GetFileName(string path)
        {
            Match _result = null;
            string _fileName = string.Empty;
            if (!string.IsNullOrEmpty(path))
            {
                if (RegexHelper.IsMatch(path, RegexPattern.FileCheck, out _result))
                    _fileName = _result.Result("${fname}") + _result.Result("${namext}") + _result.Result("${suffix}");
            }
            return _fileName;
        }
        #endregion
        #region 从路径中获取文件后缀
        /// <summary>
        /// 从路径中获取文件后缀
        /// <para>eg:FileHelper.GetFileEx(@"C:\yanzhiwei.docx");==>".docx"</para>
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns>文件后缀</returns>
        public static string GetFileEx(string path)
        {
            Match _result = null;
            string _fileName = string.Empty;
            if (!string.IsNullOrEmpty(path))
            {
                if (RegexHelper.IsMatch(path, RegexPattern.FileCheck, out _result))
                    _fileName = _result.Result("${suffix}");
            }
            return _fileName;
        }
        #endregion
        #region  获取除文件外的路径
        /// <summary>
        /// 获取除文件外的路径
        /// <para>eg:FileHelper.GetExceptName(@"C:\yanzhiwei.docx");==>"C:\"</para>
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns>除文件外的路径</returns>
        public static string GetExceptName(string path)
        {
            Match _result = null;
            string _fileName = string.Empty;
            if (!string.IsNullOrEmpty(path))
            {
                if (RegexHelper.IsMatch(path, RegexPattern.FileCheck, out _result))
                    _fileName = _result.Result("${fpath}");
            }
            return _fileName;
        }
        #endregion
        #region 获取除后缀外的路径
        /// <summary>
        /// 获取除后缀外的路径
        /// <para>eg:FileHelper.GetExceptEx(@"C:\yanzhiwei.docx");==>"C:\yanzhiwei"</para>
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns>除后缀外的路径</returns>
        public static string GetExceptEx(string path)
        {
            Match _result = null;
            string _fileName = string.Empty;
            if (!string.IsNullOrEmpty(path))
            {
                if (RegexHelper.IsMatch(path, RegexPattern.FileCheck, out _result))
                    _fileName = _result.Result("${fpath}") + _result.Result("${fname}") + _result.Result("${namext}");
            }
            return _fileName;
        }
        #endregion
        #region 将文件转换成二进制数组
        /// <summary>
        /// 将文件转换成二进制数组
        /// <para>eg:FileHelper.ToBytes(@"C:\demo.txt");</para>
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>二进制数组</returns>
        public static byte[] ToBytes(string filePath)
        {
            if (File.Exists(filePath))
            {
                using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    byte[] _buffur = new byte[stream.Length];
                    stream.Read(_buffur, 0, (int)stream.Length);
                    return _buffur;
                }
            }
            return null;
        }
        #endregion
        #region 将byte[]导出到文件
        /// <summary>
        /// 将byte[]导出到文件
        /// <para>eg: FileHelper.ToFile(_bytes, _outputFilePath); </para>
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="filePath"></param>
        public static void ToFile(byte[] bytes, string filePath)
        {
            File.WriteAllBytes(filePath, bytes);
        }
        #endregion
        #region 创建临时文件路径
        /// <summary>
        /// 创建临时文件路径
        /// <para>eg:FileHelper.CreateTempPath("jpg");</para>
        /// </summary>
        /// <param name="extension">文件后缀;eg:xml</param>
        /// <returns>临时文件路径</returns>
        public static string CreateTempPath(string extension)
        {
            string _path = Path.GetTempFileName();
            return Path.ChangeExtension(_path, extension);
        }
        #endregion
        #region 打开文件或者文件夹
        /// <summary>
        /// 打开文件或者文件夹
        /// </summary>
        /// <param name="path">文件夹或者文件的路径</param>
        public static void OpenFolderAndFile(string path)
        {
            if (File.Exists(path) || Directory.Exists(path))
            {
                //ProcessStartInfo _psi = new ProcessStartInfo("Explorer.exe");
                //_psi.Arguments = " /select," + path;
                //Process.Start(_psi);
                Process.Start(path);
            }
        }
        #endregion
        #region 获取文件大小—字节
        /// <summary>
        /// 获取文件大小—字节
        /// <para>eg:FileHelper.GetSize(TestFilePath);</para>
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件大小</returns>
        public static long GetSize(string filePath)
        {
            long _size = 0;
            try
            {
                if (File.Exists(filePath))
                {
                    FileStream _stream = new FileStream(filePath, FileMode.Open);
                    _size = _stream.Length;
                    _stream.Close();
                    _stream.Dispose();
                }
            }
            catch (Exception ex)
            {
                _size = 0;
                Debug.WriteLine(string.Format("获取文件大小异常，原因：{0}", ex.Message));
            }
            return _size;
        }
        #endregion
        #region 获取文件大小—kb
        /// <summary>
        /// 获取文件大小—kb
        /// <para>eg:FileHelper.GetKBSize(TestFilePath);</para>
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件大小_kb</returns>
        public static double GetKBSize(string filePath)
        {
            double _kb = 0;
            long _size = GetSize(filePath);
            if (_size != 0)
            {
                _kb = _size / 1024d;
            }
            return _kb;
        }
        #endregion
        #region 获取文件大小—mb
        /// <summary>
        /// 获取文件大小—mb
        /// <para>eg:FileHelper.GetMBSize(TestFilePath);</para>
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件大小_mb</returns>
        public static double GetMBSize(string filePath)
        {
            double _mb = 0;
            long _size = GetSize(filePath);
            if (_size != 0)
            {
                _mb = _size / 1048576d;//1024*1024==1048576;
            }
            return _mb;
        }
        #endregion
        #region 文件复制备份
        /// <summary>
        /// 文件复制备份【同目录下】
        /// <para>eg:FileHelper.CopyToBak(TestFilePath);</para>
        /// </summary>
        /// <param name="filePath">文件路径</param>
        public static bool CopyToBak(string filePath)
        {
            bool _result = true;
            try
            {
                int _fileCount = 0;
                string _bakName = "";
                do
                {
                    _fileCount++;
                    _bakName = string.Format("{0}.{1}.bak", filePath, _fileCount);
                }
                while (File.Exists(_bakName));
                File.Copy(filePath, _bakName);
                File.Delete(filePath);
                _result = true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(string.Format("文件复制备份，原因：{0}", ex.Message));
                _result = false;
            }
            return _result;
        }
        #endregion
        #region 复制指定目录的所有文件,不包含子目录及子目录中的文件
        /// <summary>
        /// 复制指定目录的所有文件,不包含子目录及子目录中的文件
        /// </summary>
        /// <param name="sourceDir">原始目录</param>
        /// <param name="targetDir">目标目录</param>
        /// <param name="overWrite">如果为true,表示覆盖同名文件,否则不覆盖</param>
        public static void CopyFiles(string sourceDir, string targetDir, bool overWrite)
        {
            CopyFiles(sourceDir, targetDir, overWrite, false);
        }
        #endregion
        #region 复制指定目录的所有文件
        /// <summary>
        /// 路径分割符
        /// </summary>
        private const string PATH_SPLIT_CHAR = "\\";
        /// <summary>
        /// 复制指定目录的所有文件
        /// </summary>
        /// <param name="sourceDir">原始目录</param>
        /// <param name="targetDir">目标目录</param>
        /// <param name="overWrite">如果为true,覆盖同名文件,否则不覆盖</param>
        /// <param name="copySubDir">如果为true,包含目录,否则不包含</param>
        public static void CopyFiles(string sourceDir, string targetDir, bool overWrite, bool copySubDir)
        {
            //复制当前目录文件
            foreach (string sourceFileName in Directory.GetFiles(sourceDir))
            {
                string targetFileName = Path.Combine(targetDir, sourceFileName.Substring(sourceFileName.LastIndexOf(PATH_SPLIT_CHAR) + 1));
                if (File.Exists(targetFileName))
                {
                    if (overWrite == true)
                    {
                        File.SetAttributes(targetFileName, FileAttributes.Normal);
                        File.Copy(sourceFileName, targetFileName, overWrite);
                    }
                }
                else
                {
                    File.Copy(sourceFileName, targetFileName, overWrite);
                }
            }
            //复制子目录
            if (copySubDir)
            {
                foreach (string sourceSubDir in Directory.GetDirectories(sourceDir))
                {
                    string targetSubDir = Path.Combine(targetDir, sourceSubDir.Substring(sourceSubDir.LastIndexOf(PATH_SPLIT_CHAR) + 1));
                    if (!Directory.Exists(targetSubDir))
                        Directory.CreateDirectory(targetSubDir);
                    CopyFiles(sourceSubDir, targetSubDir, overWrite, true);
                }
            }
        }
        #endregion
        #region 复制指定目录的所有文件，并把复制的文件备份到备份目录中
        /// <summary>
        /// 复制指定目录的所有文件，并把复制的文件备份到备份目录中
        /// </summary>
        /// <param name="sourceDir">原始目录</param>
        /// <param name="targetDir">目标目录</param>
        /// <param name="overWrite">如果为true,覆盖同名文件,否则不覆盖</param>
        /// <param name="copySubDir">如果为true,包含目录,否则不包含</param>
        /// <param name="backDir">备份目录</param>
        public static void CopyFiles(string sourceDir, string targetDir, bool overWrite, bool copySubDir, string backDir)
        {
            if (!Directory.Exists(backDir))
            {
                Directory.CreateDirectory(backDir);
            }
            //复制当前目录文件
            foreach (string sourceFileName in Directory.GetFiles(sourceDir))
            {
                string targetFileName = Path.Combine(targetDir, sourceFileName.Substring(sourceFileName.LastIndexOf(PATH_SPLIT_CHAR) + 1));
                if (File.Exists(targetFileName))
                {
                    if (overWrite == true)
                    {
                        File.SetAttributes(targetFileName, FileAttributes.Normal);
                        string backFileName = Path.Combine(backDir, sourceFileName.Substring(sourceFileName.LastIndexOf(PATH_SPLIT_CHAR) + 1));
                        File.Copy(targetFileName, backFileName, true);
                        File.Copy(sourceFileName, targetFileName, overWrite);
                    }
                }
                else
                {
                    File.Copy(sourceFileName, targetFileName, overWrite);
                }
            }
            //复制子目录
            if (copySubDir)
            {
                foreach (string sourceSubDir in Directory.GetDirectories(sourceDir))
                {
                    string targetSubDir = Path.Combine(targetDir, sourceSubDir.Substring(sourceSubDir.LastIndexOf(PATH_SPLIT_CHAR) + 1));
                    if (!Directory.Exists(targetSubDir))
                        Directory.CreateDirectory(targetSubDir);
                    string backSubDir = Path.Combine(backDir, targetSubDir.Substring(targetSubDir.LastIndexOf(PATH_SPLIT_CHAR) + 1));
                    if (!Directory.Exists(backSubDir))
                        Directory.CreateDirectory(backSubDir);
                    CopyFiles(sourceSubDir, targetSubDir, overWrite, true, backSubDir);
                }
            }
        }
        #endregion
        #region  剪切指定目录的所有文件,不包含子目录
        /// <summary>
        /// 剪切指定目录的所有文件,不包含子目录
        /// </summary>
        /// <param name="sourceDir">原始目录</param>
        /// <param name="targetDir">目标目录</param>
        /// <param name="overWrite">如果为true,覆盖同名文件,否则不覆盖</param>
        public static void MoveFiles(string sourceDir, string targetDir, bool overWrite)
        {
            MoveFiles(sourceDir, targetDir, overWrite, false);
        }
        #endregion
        #region 剪切指定目录的所有文件
        /// <summary>
        /// 剪切指定目录的所有文件
        /// </summary>
        /// <param name="sourceDir">原始目录</param>
        /// <param name="targetDir">目标目录</param>
        /// <param name="overWrite">如果为true,覆盖同名文件,否则不覆盖</param>
        /// <param name="moveSubDir">如果为true,包含目录,否则不包含</param>
        public static void MoveFiles(string sourceDir, string targetDir, bool overWrite, bool moveSubDir)
        {
            //移动当前目录文件
            foreach (string sourceFileName in Directory.GetFiles(sourceDir))
            {
                string targetFileName = Path.Combine(targetDir, sourceFileName.Substring(sourceFileName.LastIndexOf(PATH_SPLIT_CHAR) + 1));
                if (File.Exists(targetFileName))
                {
                    if (overWrite == true)
                    {
                        File.SetAttributes(targetFileName, FileAttributes.Normal);
                        File.Delete(targetFileName);
                        File.Move(sourceFileName, targetFileName);
                    }
                }
                else
                {
                    File.Move(sourceFileName, targetFileName);
                }
            }
            if (moveSubDir)
            {
                foreach (string sourceSubDir in Directory.GetDirectories(sourceDir))
                {
                    string targetSubDir = Path.Combine(targetDir, sourceSubDir.Substring(sourceSubDir.LastIndexOf(PATH_SPLIT_CHAR) + 1));
                    if (!Directory.Exists(targetSubDir))
                        Directory.CreateDirectory(targetSubDir);
                    MoveFiles(sourceSubDir, targetSubDir, overWrite, true);
                    Directory.Delete(sourceSubDir);
                }
            }
        }
        #endregion
        #region 删除指定目录的所有文件，不包含子目录
        /// <summary>
        /// 删除指定目录的所有文件，不包含子目录
        /// </summary>
        /// <param name="targetDir">操作目录</param>
        public static void DeleteFiles(string targetDir)
        {
            DeleteFiles(targetDir, false);
        }
        #endregion
        #region 删除指定目录的所有文件和子目录
        /// <summary>
        /// 删除指定目录的所有文件和子目录
        /// </summary>
        /// <param name="targetDir">操作目录</param>
        /// <param name="delSubDir">如果为true,包含对子目录的操作</param>
        public static void DeleteFiles(string targetDir, bool delSubDir)
        {
            foreach (string fileName in Directory.GetFiles(targetDir))
            {
                File.SetAttributes(fileName, FileAttributes.Normal);
                File.Delete(fileName);
            }
            if (delSubDir)
            {
                DirectoryInfo dir = new DirectoryInfo(targetDir);
                foreach (DirectoryInfo subDi in dir.GetDirectories())
                {
                    DeleteFiles(subDi.FullName, true);
                    subDi.Delete();
                }
            }
        }
        #endregion
        #region 创建指定目录
        /// <summary>
        /// 创建指定目录
        /// </summary>
        /// <param name="targetDir"></param>
        public static void CreateDirectory(string targetDir)
        {
            DirectoryInfo dir = new DirectoryInfo(targetDir);
            if (!dir.Exists)
                dir.Create();
        }
        #endregion
        #region 建立子目录
        /// <summary>
        /// 建立子目录
        /// </summary>
        /// <param name="parentDir">目录路径</param>
        /// <param name="subDirName">子目录名称</param>
        public static void CreateDirectory(string parentDir, string subDirName)
        {
            CreateDirectory(parentDir + PATH_SPLIT_CHAR + subDirName);
        }
        #endregion
        #region 删除指定目录
        /// <summary>
        /// 删除指定目录
        /// </summary>
        /// <param name="targetDir">目录路径</param>
        public static void DeleteDirectory(string targetDir)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(targetDir);
            if (dirInfo.Exists)
            {
                DeleteFiles(targetDir, true);
                dirInfo.Delete(true);
            }
        }
        #endregion
        #region 删除指定目录的所有子目录,不包括对当前目录文件的删除
        /// <summary>
        /// 删除指定目录的所有子目录,不包括对当前目录文件的删除
        /// </summary>
        /// <param name="targetDir">目录路径</param>
        public static void DeleteSubDirectory(string targetDir)
        {
            foreach (string subDir in Directory.GetDirectories(targetDir))
            {
                DeleteDirectory(subDir);
            }
        }
        #endregion
        #region 设置程序开机启动_注册表形式
        /// <summary>
        /// 设置程序开机启动_注册表形式
        ///<para>参考</para> 
        ///<para>http://syxc.iteye.com/blog/673972</para> 
        ///<para> http://zouqinghua11111.blog.163.com/blog/static/67997654201242334620628/ </para> 
        ///<para> http://stackoverflow.com/questions/5089601/run-the-application-at-windows-startup </para> 
        ///<para> 其他 </para> 
        ///<para> 管理员权限问题：</para> 
        ///<para> 在打开的工程中，看下Properties 下面是否有app.manifest 这个文件，如果没有，右击工程在菜单中选择“属性”，</para> 
        ///<para> 选中"Security"，在界面中勾选"Enable ClickOnce Security Settings"后，在Properties下就有自动生成app.manifest文件。</para> 
        ///<para> 打开app.manifest文件，将 </para> 
        ///<para> <requestedExecutionLevel level="asInvoker" uiAccess="false" /></para> 
        ///<para> 改为</para> 
        ///<para> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /></para> 
        ///<para> 然后在"Security"中再勾去"Enable ClickOnce Security Settings"后，重新编译即可。</para> 
        /// </summary>
        /// <param name="path">需要开机启动的exe路径</param>
        /// <param name="keyName">注册表中键值名称</param>
        /// <param name="set">true设置开机启动，false取消开机启动</param>
        public static void StartupSet(string path, string keyName, bool set)
        {
            RegistryKey _reg = Registry.LocalMachine;
            try
            {
                RegistryKey _run = _reg.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                if (set)
                {
                    _run.SetValue(keyName, path);
                }
                else
                {
                    Object _value = _run.GetValue(keyName);
                    Trace.WriteLine("StartupSet Finded :" + _value == null ? "Null" : _value);
                    if (_value != null)
                        _run.DeleteValue(keyName);
                }
            }
            finally
            {
                _reg.Close();
            }
        }
        #endregion
        #region  递归获取文件夹目录下文件
        /// <summary>
        /// 递归获取文件夹目录下文件
        /// </summary>
        /// <param name="pathName">需要递归遍历的文件夹</param>
        /// <param name="fileRule">遍历规则『委托』</param>
        public static void LoopFolder(string pathName, Action<FileInfo> fileRule)
        {
            Queue<string> _pathQueue = new Queue<string>();
            _pathQueue.Enqueue(pathName);
            while (_pathQueue.Count > 0)
            {
                string _path = _pathQueue.Dequeue();
                DirectorySecurity _pathSecurity = new DirectorySecurity(_path, AccessControlSections.Access);
                if (!_pathSecurity.AreAccessRulesProtected)//文件夹权限是否可访问
                {
                    DirectoryInfo _directoryInfo = new DirectoryInfo(_path);
                    foreach (DirectoryInfo diChild in _directoryInfo.GetDirectories())
                    {
                        _pathQueue.Enqueue(diChild.FullName);
                    }
                    foreach (FileInfo file in _directoryInfo.GetFiles())
                    {
                        fileRule(file);
                    }
                }
            }
        }
        #endregion
        #region  判断文件路径中是否包含非法字符
        /// <summary>
        /// 判断文件路径中是否包含非法字符
        /// <para>eg:FileHelper.InvalidFileNameChars(@"C:\Users\Administrator\Downloads.\12.txt");==>false</para>
        /// <para>参考:http://msdn.microsoft.com/en-us/library/aa365247.aspx </para>
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <returns>是否包含非法字符</returns>
        public static bool InvalidFileNameChars(this string fileName)
        {
            bool _checkResult = true;
            if (!string.IsNullOrEmpty(fileName))
            {
                string _invalidFileNameChars = new string(Path.GetInvalidFileNameChars());
                Regex _regex = new Regex("[" + Regex.Escape(_invalidFileNameChars) + "]");
                _checkResult = !_regex.IsMatch(fileName);
            }
            return _checkResult;
        }
        #endregion
        #region 复制本机大文件
        /// <summary>
        /// 复制本机大文件
        /// <para>参考：http://www.cnblogs.com/zfanlong1314/p/3922803.html </para>
        /// <para>eg:FileHelper.CopyLocalBigFile(@"C:\Users\YanZh_000\Downloads\TheInterview.mp4", @"D:\The Interview(1080p).mp4", 1024 * 1024 * 5))</para>
        /// </summary>
        /// <param name="fromPath">源文件的路径</param>
        /// <param name="toPath">文件保存的路径</param>
        /// <param name="eachReadLength">每次读取的长度</param>
        /// <returns>是否复制成功</returns>
        public static bool CopyLocalBigFile(string fromPath, string toPath, int eachReadLength)
        {
            /*
             * 参考：
             * FileStream缓冲读取和写入可以提高性能。FileStream读取文件的时候，是先讲流放入内存，经Flash()方法后将内存中（缓冲中）的数据写入件。如果文件非常大，势必消耗性能。
             */
            bool _copyResult = true;
            //将源文件 读取成文件流
            FileStream _fromFile = new FileStream(fromPath, FileMode.Open, FileAccess.Read);
            //已追加的方式 写入文件流
            FileStream _toFile = new FileStream(toPath, FileMode.Append, FileAccess.Write);
            try
            {
                //实际读取的文件长度
                int _toCopyLength = 0;
                //如果每次读取的长度小于 源文件的长度 分段读取
                if (eachReadLength < _fromFile.Length)
                {
                    byte[] _buffer = new byte[eachReadLength];
                    long _copied = 0;
                    while (_copied <= _fromFile.Length - eachReadLength)
                    {
                        _toCopyLength = _fromFile.Read(_buffer, 0, eachReadLength);
                        _fromFile.Flush();
                        _toFile.Write(_buffer, 0, eachReadLength);
                        _toFile.Flush();
                        //流的当前位置
                        _toFile.Position = _fromFile.Position;
                        _copied += _toCopyLength;
                    }
                    int _left = (int)(_fromFile.Length - _copied);
                    _toCopyLength = _fromFile.Read(_buffer, 0, _left);
                    _fromFile.Flush();
                    _toFile.Write(_buffer, 0, _left);
                    _toFile.Flush();
                }
                else
                {
                    //如果每次拷贝的文件长度大于源文件的长度 则将实际文件长度直接拷贝
                    byte[] _buffer = new byte[_fromFile.Length];
                    _fromFile.Read(_buffer, 0, _buffer.Length);
                    _fromFile.Flush();
                    _toFile.Write(_buffer, 0, _buffer.Length);
                    _toFile.Flush();
                }
            }
            catch (Exception)
            {
                _copyResult = false;
            }
            finally
            {
                _fromFile.Close();
                _toFile.Close();
            }
            return _copyResult;
        }
        #endregion
    }
}